<?php
    include('header.php');
    
    $db = db_connect();
    
    $sql = 'SELECT * FROM news_promotion WHERE news_active=1 ORDER BY news_date DESC LIMIT 5';
    $result = mysql_query($sql);
    $i = 1;
    $news_promotion = '';
    
    while($row=mysql_fetch_assoc($result)){
	$date = date("d/m/Y",strtotime($row['news_date']));
	
	if(file_exists('images/news-promotion/'.$row['news_img']) && $row['news_img']!=''){
	    $img = '<img src="images/news-promotion/'.$row['news_img'].'" />';
	    $img_class = ' current_news';
	}else{
	    $img = '';
	    $img_class = ' old_news_img';
	}
	
	if($i<=1){
	    $news_promotion .=<<<news_promotion
	    <div id="first_news" class="news_box">
	    <div class="news_img{$img_class}">
		{$img}
	    </div>
	    <div class="news_details" id="news_detail_open">
		<p class="news_date">{$date}</p>
		<p class="news_detail">
		    {$row['news_detail']}
		</p>
	    </div>
	    <div class="clear">&nbsp;</div>
	</div>
news_promotion;
	}else{
	    $news_promotion .=<<<news_promotion
	    <div id="" class="news_box">
	    <div class="news_img old_news_img">
		{$img}
	    </div>
	    <div class="news_details old_news_details" id="news_detail_close">
		<p class="news_date">{$date}</p>
		<p class="news_detail">
		    {$row['news_detail']}
		</p>
	    </div>
	    <div class="clear">&nbsp;</div>
	</div>
news_promotion;
	}
	
	$i++;
    }
    
    db_close($db);
?>
    <div id="news_promotion">
        <h3>ข่าวสารและโปรโมชั่น</h3>
        
	<?php echo $news_promotion; ?>
    </div>
    <script language="javascript" type="text/javascript" src="js/js.func.js"></script>
    <script type="text/javascript">
	$(document).ready(function(){
	    $('.news_details').click(function(){
		var thisEle = $(this);
		var thisNewsImg = $(this).parent().find('.news_img');
		
		if(trim(thisNewsImg.html())!=''){
		    if(thisEle.attr('id')=='news_detail_close'){
			var options = {};
			thisNewsImg.show('fold',options,800);
			thisNewsImg.removeClass('old_news_img');
			thisEle.removeClass('old_news_details');
			thisEle.attr('id','news_detail_open');
		    }else{
			var options = {};
			thisNewsImg.hide('fold',options,800,function(){
			    thisNewsImg.addClass('old_news_img');
			    thisEle.addClass('old_news_details');
			});
			
			thisEle.attr('id','news_detail_close');
		    }
		}
		
	    });
	});
    </script>
<?php
    include('footer.php');
?>